<template>
	<div class="logview allbgc">
		<!-- {{$store.state.a}} -->
		<div class="logview-top logreg-top alltop">
			<span>
				<router-link to="/"><</router-link>
			</span>
			<p>
				<span>
					<router-link to="/reg">注册</router-link>
					/
					<router-link to="/log">登录</router-link>
				</span>
			</p>
		</div>

		<div class="logview-from logreg-from">
			<label>
				<input type="text" placeholder="请输入手机号" v-model="username" />
			</label>
			<label>
				<input type="password" placeholder="请输入密码" v-model="password" />
				<button>
					<router-link to="/forget">忘记密码</router-link>
				</button>
			</label>
		</div>
		<div class="photo">
			<button @click="log">登录</button>
		</div>
	</div>
</template>

<script>
	export default {
		name:"login",
		data() {
			return {
				username: "",
				password: ""
			}
		},
		methods: {
			log() {
				console.log(this.username, this.password)
				if (this.username == "") {
					this.$toast('手机号不能为空');
					return false
				}
				if (this.password == "") {
					this.$toast('密码不能为空');
					return false
				}
				this.$http.post("/user/login", {
					username: this.username,
					password: this.password
				}).then(res => {
					console.log(res)
					this.cate=res.data.data
					// 在这里进行成功或者错误的判断
					if (res.data.code === 0) {
						// 注册成功
						this.$toast('登录成功');
						let {token,uid,userInfo} =res
						localStorage.setItem("token",token)
						localStorage.setItem("uid",uid)
						localStorage.setItem("userInfo",JSON.stringify(userInfo))
						
						// 把登陆以后的token,uid,userInfo的数据存到vuex里
						this.$store.commit('change',{key:'uid',value:uid})
						this.$store.commit('change',{key:'token',value:token})
						this.$store.commit('change',{key:'userInfo',value:userInfo})
						this.$store.commit('change',{key:'isLogin',value:true})
						
						
						setTimeout(v=>{
							this.$router.back()
						},500)
						return false
					} 
					if(res.data.code === 10002){
						// 失败
						this.$toast('用户不存在');
					}
				}).catch(err => {
					console.log(err)
					if (this.response.status == 401) {
						this.$toast('手机号或密码错误');
					}
					if (this.response.status == 404) {
						this.$toast('网络请求失败');
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.logview-from label button {
		top: 63px;
		background-color: #fff;
	}
</style>